const express = require('express');
const router = express.Router();
const Friend = require('../models/Friend');
const auth = require('../middleware/auth');

// 发送好友请求
router.post('/request', auth, async (req, res) => {
  try {
    const { to } = req.body;
    let friendRecord = await Friend.findOne({ user: req.user.id });
    if (!friendRecord) {
      friendRecord = new Friend({ user: req.user.id });
    }
    friendRecord.friendRequests.push({ from: req.user.id, to, status: 'pending' });
    await friendRecord.save();
    res.status(201).json(friendRecord);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

// 获取好友请求
router.get('/requests', auth, async (req, res) => {
  try {
    const friendRecord = await Friend.findOne({ user: req.user.id });
    if (!friendRecord) {
      return res.status(404).json({ error: 'No friend record found' });
    }
    res.json(friendRecord.friendRequests);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

// 接受好友请求
router.post('/accept/:id', auth, async (req, res) => {
  try {
    const { id } = req.params;
    const friendRecord = await Friend.findOne({ user: req.user.id });
    if (!friendRecord) {
      return res.status(404).json({ error: 'No friend record found' });
    }
    const request = friendRecord.friendRequests.find(
      (req) => req._id.toString() === id && req.status === 'pending'
    );
    if (request) {
      request.status = 'accepted';
      friendRecord.friends.push(request.from);
      await friendRecord.save();
    }
    res.json(friendRecord);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

// 拒绝好友请求
router.post('/reject/:id', auth, async (req, res) => {
  try {
    const { id } = req.params;
    const friendRecord = await Friend.findOne({ user: req.user.id });
    if (!friendRecord) {
      return res.status(404).json({ error: 'No friend record found' });
    }
    const request = friendRecord.friendRequests.find(
      (req) => req._id.toString() === id && req.status === 'pending'
    );
    if (request) {
      request.status = 'rejected';
      await friendRecord.save();
    }
    res.json(friendRecord);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

// 获取好友列表
router.get('/list', auth, async (req, res) => {
  try {
    const friendRecord = await Friend.findOne({ user: req.user.id });
    if (!friendRecord) {
      return res.status(404).json({ error: 'No friend record found' });
    }
    res.json(friendRecord.friends);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

// 删除好友
router.delete('/:id', auth, async (req, res) => {
  try {
    const { id } = req.params;
    const friendRecord = await Friend.findOne({ user: req.user.id });
    if (!friendRecord) {
      return res.status(404).json({ error: 'No friend record found' });
    }
    friendRecord.friends = friendRecord.friends.filter(
      (friend) => friend.toString() !== id
    );
    await friendRecord.save();
    res.json(friendRecord);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

module.exports = router;